﻿using System;
using System.Collections.Generic;
using System.IO;

class Solution
{
    static void Main(String[] args)
    {
        int T = int.Parse(Console.ReadLine());
        for (int t = 1; t <= T; ++t)
        {
            Solve(t);
        }
        //Console.ReadKey();
    }

    private static void Solve(int t)
    {
        int n = int.Parse(Console.ReadLine());
        int[] prices = new int[n];
        int[] right = new int[n];
        String[] s = Console.ReadLine().Split(new[] { ' ' });
        for (int i = 0; i < n; ++i)
        {
            prices[i] = int.Parse(s[i]);
            right[i] = i;
        }
        for (int i = n - 2, r = n - 1; i >= 0; --i)
        {
            if (prices[i] <= prices[r])
            {
                right[i] = r;
            }
            else
            {
                r = i;
            }
        }
        long total = 0, stock = 0;
        for (int i = 0; i < n; ++i)
        {
            if (right[i] == i)
            {
                total += stock * prices[i];
                stock = 0;
            }
            else
            {
                stock += 1;
                total -= prices[i];
            }
        }
        Console.WriteLine(total);
    }
}